<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\PatientService;
use Illuminate\Http\Request;

class PatientController extends BaseController
{
    //患者

    public function __construct(PatientService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->getList($uid);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //添加
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $pas['realname'] = $request->post('realname', '');
        $pas['age'] = $request->post('age', 0);
        $pas['sex'] = $request->post('sex', 0);
        $pas['title'] = $request->post('title', '');
        $pas['detail'] = $request->post('desc', '');
        $imgList = $request->post('imgList', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
//        if (!$pas['realname']) {
//            return $this->toJson(-2, '姓名必须的');
//        }
//        if (!$pas['age']) {
//            return $this->toJson(-2, '年龄必须的');
//        }
//        if (!in_array($pas['sex'], [1, 2])) {
//            return $this->toJson(-2, '性别错误');
//        }
        if (!$pas['title']) {
            return $this->toJson(-2, '病历名称必须的');
        }
        $res = $this->db->add($pas, $imgList);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $token = $request->post('token', '');
        $id = $request->post('id', 0);
        $pas['realname'] = $request->post('realname', '');
        $pas['age'] = $request->post('age', 0);
        $pas['sex'] = $request->post('sex', 0);
        $pas['title'] = $request->post('title', '');
        $pas['detail'] = $request->post('desc', '');
        $imgList = $request->post('imgList', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$id) {
            return $this->toJson(-2, '没有ID');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '病历名称必须的');
        }
        $res = $this->db->modify($id, $pas, $imgList);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}